<?php
class User extends AppModel {

	var $name = 'User';
	var $primaryKey = 'user';
	var $actsAs = array('Acl' => array('requester'));
	var $validate = array(
		'username' => array(
			'isUnique' => array(
				'rule' => 'isUnique',
				'required' => true,
				'message' => 'El username ya existe'),
			'notEmpty' => array(	
				'rule' => 'notEmpty',
				'required' => true,
				'message' => 'El username no puede estar vacio')
			),
		'email' => array(
			'email' => array(
				'rule' => 'email',
				'required' => true,
				'message' => 'Por favor escriba un email valido'),
			'isUnique' => array(
				'rule' => 'isUnique',
				'required' => true,
				'message' => 'El email ya existe'),
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'required' => true,
				'message' => 'El email no puede estar vacio')
			),
		'fechacreacion' => array(
			'date' => array(
				'rule' => 'date',
				'required' => true,
				'message' => 'La fechaCreacion no puede estar vacio')
			),
		/*'photo_size' => array(*/
			/*'limit' => array(
				'rule' => array('comparison', '<=', 10240))), //10kb*/
		'photo_type' => array('type' => array('rule' => array('comparison', '==', 'image/jpeg')))
	);

	
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'Paise' => array('className' => 'Paise',
								'foreignKey' => 'pais_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			),
			'Grupo' => array('className' => 'Grupo',
								'foreignKey' => 'grupo_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			),
			'Equipo' => array('className' => 'Equipo',
								'foreignKey' => 'equipo_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			)
	);

	var $hasMany = array(
			'Cambio' => array('className' => 'Cambio',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			),
			'Tema' => array('className' => 'Tema',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			),
			'Topico' => array('className' => 'Topico',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			),
			'Post' => array('className' => 'Post',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			),
			'Partido' => array('className' => 'Partido',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			)
	);

	var $hasAndBelongsToMany = array(
/*			'Partido' => array('className' => 'Partido',
						'joinTable' => 'partidos_users',
						'foreignKey' => 'user_id',
						'associationForeignKey' => 'partido_id',
						'unique' => true,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'finderQuery' => '',
						'deleteQuery' => '',
						'insertQuery' => ''
			),
			'Sala' => array('className' => 'Sala',
						'joinTable' => 'salas_users',
						'foreignKey' => 'user_id',
						'associationForeignKey' => 'sala_id',
						'unique' => true,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'finderQuery' => '',
						'deleteQuery' => '',
						'insertQuery' => ''
			),*/
/*			'ObservaPartido' => array('className' => 'ObservaPartido',
						'joinTable' => 'users_observa_partidos',
						'foreignKey' => 'user_id',
						'associationForeignKey' => 'observa_partido_id',
						'unique' => true,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'finderQuery' => '',
						'deleteQuery' => '',
						'insertQuery' => ''
			)*/
	);
	
	function parentNode() {
	    if (!$this->id && empty($this->data)) {
		return null;
	    }
	    $data = $this->data;
	    if (empty($this->data)) {
		$data = $this->read();
	    }
	    if (!$data['User']['grupo_id']) {
		return null;
	    } else {
		return array('Grupo' => array('grupo' => $data['User']['grupo_id']));
	    }
	}
	
	function buscarUsuarios( $data= null){
		$userNombre='';
		$userApellido='';
		$userUsername='';
		//print_r($data);
		if($data['User']['BuNombre']==1){
			$userNombre=$data['User']['nombre'];
		}
		if($data['User']['BuApellido']==1){
			 $userApellido=$data['User']['nombre'];
		}
		if($data['User']['BuUsername']==1){
			 $userUsername=$data['User']['nombre'];
		}
		$usuarios=$this->find('all', array('conditions'=>array (
			 'OR' => array("User.username LIKE" => "%".$userUsername."%",
				array(
				  "or" => array (
				"User.nombre LIKE" => "%".$userNombre."%",
				"User.apellido LIKE" => "%".$userApellido."%"
				)))
			       )
			));
		print_r($usuarios);
		
	}
	
	function topFiveUsers()
	{
		$topFive=$this->find('all', array('fields'=>array('User.puntos','User.username'),'order'=>'User.puntos DESC','limit'=>5));
		//print_r($topFive);
		return $topFive;
	}
	
	function nombreUser($userId)
	{
		$nombreUser=$this->find('all', array('fields'=>array('User.nombre','User.username','User.apellido'),'conditions' => array('User.user =' => $userId)));
		///print_r($nombreUser);
		return $nombreUser;
		
	}

	function topPorcentajeWinners($tipo=1){
		if($tipo==1){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.puntos DESC'),'limit' => 30));
		}elseif($tipo==2){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.partidosjugados DESC'),'limit' => 30));
		}elseif($tipo==3){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.partidosganados DESC'),'limit' => 30 ));
		}elseif($tipo==4){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.partidosempatados DESC'),'limit' => 30 ));
		}
		elseif($tipo==5 || $tipo==6){
			$users=$this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3)));
			foreach ($users as $user){
				if($tipo==5){ //partidos ganados/partidos jugados
					if($user['User']['partidosjugados']>0){
						$this->id = $user['User']['user']; 
						$this->saveField('aux',$user['User']['partidosganados']/$user['User']['partidosjugados']);
					}else{
						$this->id = $user['User']['user']; 
						$this->saveField('aux',0);
					}
				}elseif($tipo==6){ //partidos perdidos/partidos jugados
					if($user['User']['partidosjugados']>0){
						$this->id = $user['User']['user']; 
						$this->saveField('aux',$user['User']['partidosperdidos']/$user['User']['partidosperdidos']);
					}else{
						$this->id = $user['User']['user']; 
						$this->saveField('aux',0);
					}
				}
			}
			$users=$this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3), 'order' => array('User.aux DESC'),'limit' => 30));
			return $users;
		}elseif($tipo==11){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.golesfavor DESC'),'limit' => 30 ));
		}elseif($tipo==12){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.golesencontra DESC'),'limit' => 30 ));
		}elseif($tipo==13){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.golesfavor ASC'),'limit' => 30 ));
		}elseif($tipo==14){
			return $this->find('all', array('conditions'=>array('User.estado >'=>0, 'User.grupo_id <'=>3),'order' => array('User.golesencontra ASC'),'limit' => 30 ));
		}
	}
	
	function misDatosUser($userId)
	{
		$misDatos=$this->find('first', array('conditions' => array('User.user =' => $userId)));
		///print_r($nombreUser);
		return $misDatos;
		
	}
}
?>